var util = require('../../libs/util.js');
var app = getApp()
Page({
  data: {
    hasData: false,
    userInfo: {},
    truename: "",
    searchShowed: false,
    searchVal: "",
    selected: false,
    dialog: false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--平台调度端" });
    var that = this;
    this.loadData();
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData("");
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData("");
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.loadData(e.detail.value);
  },
  loadData: function (val) {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("shopManage", {
      CO_ID: app.globalData.CO_ID,
      val:val
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        that.setData({
          hasData: true
        });
        var data = res.data.data, _data = [];
        for (var i = 0; i < data.length; i++) {
          _data.push({
            ID: data[i].ID,
            name: data[i].name,
            address: data[i].address,
            coord: data[i].coord
          });
        }
        that.setData({
          shopData: _data
        })
      }
    });
  },
  deleteShop: function (e) {
    var id = e.currentTarget.id;
    wx.showModal({
      title: '删除车辆',
      content: '是否确认删除门店',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候...',
          });
          util.putData("deleteShop", {
            id: id
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showLoading({
                title: "删除成功",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/schedule/shop'
                    })
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  },
  shopReposition: function (e) {
    var that = this;
    var id = e.currentTarget.id, shopData = that.data.shopData, coord;
    for (var i = 0; i < shopData.length; i++) {
      if (shopData[i].ID == id) {
        that.setData({ shop_current: shopData[i] });
        coord = String(shopData[i].coord).split(",");
      }
    }
    that.setData({
      dialog: true,
      latitude_h: '',
      latitude_m: '',
      latitude_s: '',
      longitude_h: '',
      longitude_m: '',
      longitude_s: '',
      latitude: coord[0],
      longitude: coord[1]
    })

  },
  closeDialog:function(){
    this.setData({
      dialog: false
    })
  },
  updateCoords:function(){
    var that = this;
    var shop = that.data.shop_current;
    wx.showModal({
      title: '位置重定位',
      content: '确认重置收货点（' + shop.name + '）的坐标位置吗？',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '更新坐标中...',
          });
          util.postData("pointReposition", {
            type: 'shop',
            id: shop.ID,
            coord: that.data.latitude + "," + that.data.longitude
          }, function (res, error) {
            console.log(res); console.log(error)
            if (error) {
              wx.hideLoading(); wx.showToast({ title: '定位失败', image: '/images/wrong.svg', duration: 2000 });
              return false;
            }
            if (res.data.success) {
              wx.hideLoading();
              wx.showToast({ title: '定位成功', icon: 'success', duration: 2000 });
            } else {
              wx.hideLoading(); wx.showToast({ title: '定位失败', image: '/images/wrong.svg', duration: 2000 });
            }
          });
        } else if (res.cancel) {

        }
      }
    })
  },
  gpsToMap:function(){
    //Decimal Degrees = Degrees + minutes / 60 + seconds / 3600
    //例：57°55’56.6″ =57 + 55 / 60 + 56.6 / 3600=57.9323888888888
    var _latitude = Number(this.data.latitude_h) + Number(this.data.latitude_m) / 60 + Number(this.data.latitude_s) / 3600;
    var _longitude = Number(this.data.longitude_h) + Number(this.data.longitude_m) / 60 + Number(this.data.longitude_s) / 3600;

    var locations = util.gpsTransform(Number(_latitude), Number(_longitude));
    this.setData({
      longitude: locations.lon,
      latitude: locations.lat
    });
  },
  showInMap: function () {
    wx.openLocation({
      latitude: Number(this.data.latitude),
      longitude: Number(this.data.longitude),
      scale: 28
    });
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  shopPicture: function (e) {
    var id = e.currentTarget.id, shopData = this.data.shopData;
    for (var i = 0;i<shopData.length;i++){
      if (shopData[i].ID == id){
        wx.navigateTo({
          url: '/pages/schedule/apps/shopPicture?id=' + id + '&name=' + shopData[i].name+'&address=' + shopData[i].address
        })
      }
    }
    
  },
  gotoHome: function () {
    wx.redirectTo({
      url: '/pages/schedule/schedule'
    })
  },
  gotoPathNew: function () {
    wx.redirectTo({
      url: '/pages/index/deliveryForm?delivery=true'
    })
  },
  deliveryMonitor: function (e) {
    wx.redirectTo({
      url: '/pages/receiver/receiver_monitor?id=' + e.currentTarget.id
    })
  },
  gotoMonitor: function () {
    wx.redirectTo({
      url: '/pages/schedule/monitor'
    })
  },
  gotoPath: function () {
    wx.redirectTo({
      url: '/pages/schedule/path'
    })
  },
  gotoAlarmMsg: function () {
    wx.redirectTo({
      url: '/pages/schedule/alarmMessage'
    })
  },
  gotoInfo: function () {
    wx.redirectTo({
      url: '/pages/schedule/apps'
    })
  }
})
